package com.zhike.b2b.data;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.saas.biz.dao.companyDAO.CompanyExt;
import com.zhike.b2b.base.BaseService;

@Service
public class CommenAtomic extends BaseService
{
   Logger log = Logger.getLogger(this.getClass());
   
  public String getAutoTradeID(String paramString1, String paramString2, String paramString3)
  {
    String str1 = "";
    String str2 = getCompanyByCustID(paramString1);
    try
    {
  
      String sql = "select max(substr(" + paramString3 + ",-9))" + " from " + paramString2;
      List<String> ls = jt.query(sql, new RowMapper<String>(){
         public String mapRow(ResultSet rs, int arg1) throws SQLException {
			 return rs.getString(1);
		}
      });
      if(ls!=null&&ls.size()>0)
      {
    	str1 = ls.get(0);
	    if ((null == str1) || ("".equals(str1)))
	    {
	      str1 = str2 + "100000000";
	    }
	    else
	    {
	      long l = Long.parseLong(str1) + 1L;
	      str1 = str2 + String.valueOf(l);
	    }
      }else{
    	  str1 = str2 + "100000000";
      }
    }catch(Exception ex)
    {
    	log.error("getAutoTradeID"+ex.getMessage());
    }
    return str1;
  }

  public String getCompanyByCustID(String paramString)
  {
    String str1 = "";
    String str2 = "";
    String str3 = "";
    CompanyExt localCompanyExt = new CompanyExt();
    ArrayList localArrayList = new ArrayList();
    localCompanyExt.setParam(":VCUST_ID", paramString);
    localArrayList = localCompanyExt.selByList("SEL_COMPANY_BY_CUST_ID");
    if ((localArrayList != null) && (localArrayList.size() > 0))
    {
      HashMap localHashMap = (HashMap)localArrayList.get(0);
      String str4 = localHashMap.get("eparchy_code").toString();
      String str5 = localHashMap.get("class_id").toString();
      if (!"".equals(str4))
        str3 = str4.substring(0, 6);
      if (!"".equals(str5))
        str2 = str5.substring(0, 3);
    }
    if ((str3 != "") && (str2 != ""))
      str1 = str3 + str2;
    return str1;
  }
}

